<?php


namespace App\Logic;

use App\Models\ExamModel;
use App\Models\OptionModel;
use App\Models\QuestionModel;
use App\Models\RandomModel;
use App\Utils\Singleton;
use \Illuminate\Http\Request;
use \Illuminate\Http\Response;


class StudentExamLogic
{
    use Singleton;

    public function get(Request $request,Response $response)
    {
        //解密
        $subject_id=$request->input('subject_id');
        $subject_id = decrypt($subject_id);
        $data=[];
        if (!empty($subject_id)){
            foreach (['单选题','判断题','多选题','简答题'] as $v){

                if (!empty(ExamModel::getInstance()->getList('question_id',['type'=>$v,'subject_id'=>$subject_id]))){
                    $questions=ExamModel::getInstance()->getList('question_id',['type'=>$v,'subject_id'=>$subject_id]);
                    $listData=[];
                    $questions=array_column($questions,'question_id');
                    $num=ExamModel::getInstance()->count(['type'=>$v,'subject_id'=>$subject_id]);
                    $score=objectToArray(ExamModel::getInstance()->getOne('question_score',['question_id'=>$questions[0]]));
                    $listData['score']=$score['question_score']*$num;
                    $listData['question_type']=$v;

                    foreach ($questions as $value){
                        $contents=objectToArray(QuestionModel::getInstance()->getList('question_id,question_content',['question_id' =>$value]))[0];
                        $options=objectToArray(OptionModel::getInstance()->getList('option_id,option_content',['question_id'=>$value]));
                        $contents['options']=$options;
                        //array_push($contents,$options);

                        $listData['question_list'][]=$contents;
                    }
                    $data[]=$listData;

                }elseif (!empty(RandomModel::getInstance()->getOne('num,question_score',['type'=>$v,'subject_id'=>$subject_id]))){

                    $questions=RandomModel::getInstance()->getOne('num,question_score',['type'=>$v,'subject_id'=>$subject_id]);
                    $allData=[];
                    $questions=objectToArray($questions);

                    $allData['score']=$questions['question_score']*$questions['num'];
                    $allData['question_type']=$v;
                    //$allData[]['question_list']=[];
                    //$listData[]=$allData;
                    $list=QuestionModel::getInstance()->getList('question_id',['question_type'=>$v,'subject_id'=>$subject_id]);
                    $arr=array_flip(array_column($list,'question_id'));

                    $randData=array_rand($arr,$questions['num']);
                    if (!empty($randData)){
                        foreach ($randData as $value){
                            $contents=objectToArray(QuestionModel::getInstance()->getList('question_id,question_content',['question_id' =>$value]))[0];
                            $options=objectToArray(OptionModel::getInstance()->getList('option_id,option_content',['question_id'=>$value]));

                            if (!empty($options)){
                                $contents['options']=$options;
                                //array_push($contents,$options);
                            }
                            $allData['question_list'][]=$contents;
                        }
                    }


                    $data[]=$allData;
                }
            }
        }
        return $data;

    }

}

